package ubc.cs.cpsc210.sustainabilityapp.webservices;

import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Response;
import org.scribe.oauth.OAuthService;


/**
 * Ask Yelp for food service locations
 */
public class YelpService {

	// Where we will contact yelp
	private final static String YELP_URL = "http://api.yelp.com/v2/search";

	// You will need these fields
	private OAuthService service;
	private Token accessToken;

	/**
	 * Constructor for the service
	 */
	public YelpService() {

		  String consumerKey = "OlLfqhIvgsCmm9pBCTnUmA";
		  String consumerSecret = "MGi0IPg7F3WRCc_6eWuDjGyHqzM";
		  String token = "Zr9VHfJIatMTTnSGWIWDxumei1QqwZiT";
		  String tokenSecret = "PFgKwTJQ5GJKglc-xwD19CuWNWM";
		  
		  this.service = new ServiceBuilder().provider(NullApi.class).apiKey(consumerKey).apiSecret(consumerSecret).build();
		  this.accessToken = new Token(token, tokenSecret);
		
	}
	
	/**
	 * Perform the search on Yelp
	 * 
	 * @param term
	 *            The kind of term to search Yelp for
	 * @param latitude
	 *            The latitude to center the search on
	 * @param longitude
	 *            The longitude to center the search on
	 * @return The string returned from Yelp
	 */
	public String search(double latitude, double longitude) {
		
		 OAuthRequest request = new OAuthRequest(Verb.GET, YELP_URL);
		 request.addQuerystringParameter("limit", "20");
		 request.addQuerystringParameter("category_filter", "restaurants");
		 request.addQuerystringParameter("ll", latitude + "," + longitude);
		 this.service.signRequest(this.accessToken, request);
		 Response response = request.send();
		 return response.getBody();
		
	}

	// Use this class as a parameter to the provider method you will call
	// in other methods of this class
	public static class NullApi extends DefaultApi10a {

		@Override
		public String getRequestTokenEndpoint() {
			return null;
		}

		@Override
		public String getAccessTokenEndpoint() {
			return null;
		}

		@Override
		public String getAuthorizationUrl(Token requestToken) {
			return null;
		}

	}
	
}
